package com.simon.annual.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.simon.annual.entity.ReportDebtfinancing;
import com.simon.annual.entity.ReportIndirectfinancing;
import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.entity.ReportPrefshareholder;
import com.simon.annual.entity.ReportPrefshareissue;
import com.simon.annual.entity.ReportProfitdistribution;
import com.simon.annual.entity.ReportStockissue;
import com.simon.annual.repositoy.IReportDebtfinancingRepositoy;
import com.simon.annual.repositoy.IReportIndirectfinancingRepositoy;
import com.simon.annual.repositoy.IReportNeeqRepositoy;
import com.simon.annual.repositoy.IReportPrefshareholderRepositoy;
import com.simon.annual.repositoy.IReportPrefshareissueRepositoy;
import com.simon.annual.repositoy.IReportProfitdistributionRepositoy;
import com.simon.annual.repositoy.IReportStockissueRepositoy;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IStockissue;
/**
 * 第七节:融资及分配情况
 * */
@Service
public class FindStockissueImpl implements IStockissue{
	
	@Resource
	private ICompanyPublicInfo cmpanyPublicInfo;
	@Resource
	private IReportStockissueRepositoy reportStockissueRepositoy;
	@Resource
	private IReportPrefshareissueRepositoy reportPrefshareissueRepositoy;
	@Resource
	private IReportPrefshareholderRepositoy reportPrefshareholderRepositoy;
	@Resource
	private IReportIndirectfinancingRepositoy reportIndirectfinancingRepositoy;
	@Resource
	private IReportDebtfinancingRepositoy reportDebtfinancingRepositoy;
	@Resource
	private IReportProfitdistributionRepositoy reportProfitdistributionRepositoy;
	@Resource
	private IReportNeeqRepositoy reportNeeqRepositoy;
	@Resource
	private Environment env;
	
	@Override
	public ReportNeeq analysis(Document doc, ReportNeeq reportNeeq, int companyPtrId) {
		//挂牌以来普通股股票发行情况 
		
		Element reportStockissue = doc.getElementsMatchingOwnText(env.getProperty("pdf.seven.title")).last();
		Element reportStockissueValue = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.stock"), reportStockissue, 0);
		if(reportStockissueValue!=null){
			Element stockissuev = cmpanyPublicInfo.getNextTag(reportStockissueValue, reportStockissueValue);
			Elements reportStockissueTrs = stockissuev.select("table tr");
			List<ReportStockissue> reportStockissueList = new ArrayList<ReportStockissue>();
			for (int i = 1; i < reportStockissueTrs.size(); i++) {
				Elements reportStockissueTds = reportStockissueTrs.get(i).children();
				if(reportStockissueTds.size()==12){
					ReportStockissue stockissue = new ReportStockissue();
					stockissue.setDisclosureDate(reportStockissueTds.get(0).text());
					stockissue.setIssueDate(reportStockissueTds.get(1).text());
					stockissue.setIssuePrice(reportStockissueTds.get(2).text());
					stockissue.setIssueAmount(reportStockissueTds.get(3).text());
					stockissue.setNoEmployeeIssued(reportStockissueTds.get(4).text());
					stockissue.setNoMarketMakerIssued(reportStockissueTds.get(5).text());
					stockissue.setNoExternalPersonIssued(reportStockissueTds.get(6).text());
					stockissue.setNoPrivateFundIssued(reportStockissueTds.get(7).text());
					stockissue.setNoTrustIssued(reportStockissueTds.get(8).text());
					stockissue.setIsProceedPurposeChanged(reportStockissueTds.get(9).text());
					stockissue.setRaisedValue(reportStockissueTds.get(10).text());
					stockissue.setProceedsUtilization(reportStockissueTds.get(11).text());
					reportStockissueList.add(stockissue);
				}
					reportStockissueRepositoy.save(reportStockissueList);
			}
			//募集资金使用情况
			reportNeeq.setUsedProceedsReview(reportStockissueTrs.last().text());
		}
		//二、存续至本期的优先股股票相关情况(原文本部分无此章节???)
		Element prefshare = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.prefshare"), reportStockissue, -10);
		if(prefshare!=null){
			//基本情况
			Element prefshareissue = cmpanyPublicInfo.findNextTag("table", prefshare);
			Elements  prefshareissueTrs =prefshareissue.select("table tr");
			if(prefshareissueTrs.first().children().size()==8){
				if(prefshareissueTrs.size()>1&&!StringUtil.isBlank(prefshareissueTrs.get(1).text().replace("-", ""))){
					List<ReportPrefshareissue> reportPrefshareissueList = new ArrayList<ReportPrefshareissue>();
					for (int i = 0; i < prefshareissueTrs.size(); i++) {
						if(i!=0){
							Elements prefshareissueTds = prefshareissueTrs.get(i).children();
							ReportPrefshareissue reportPrefshareissue = new ReportPrefshareissue();
							reportPrefshareissue.setSecurityCode(prefshareissueTds.get(0).text());
							reportPrefshareissue.setSecurityAbbr(prefshareissueTds.get(1).text());
							reportPrefshareissue.setIssuePrice(prefshareissueTds.get(2).text());
							reportPrefshareissue.setIssueShare(prefshareissueTds.get(3).text());
							reportPrefshareissue.setCouponRate(prefshareissueTds.get(5).text());
							reportPrefshareissue.setTradeStartDate(prefshareissueTds.get(6).text());
							reportPrefshareissue.setTradeEndDate(prefshareissueTds.get(7).text());
							reportPrefshareissue.setProjectScopeId(companyPtrId);
							reportPrefshareissueList.add(reportPrefshareissue);
						}
					}
					reportPrefshareissueRepositoy.save(reportPrefshareissueList);
				}
				//股东情况
				Element shareholder = cmpanyPublicInfo.findNextTag("table", prefshareissue);
				Elements shareholderTrs = shareholder.select("table tr");
				if(shareholderTrs.size()>3&&!StringUtil.isBlank(shareholderTrs.get(3).text().replace("-", ""))){
					List<ReportPrefshareholder> reportPrefshareholderList = new ArrayList<ReportPrefshareholder>();
					for (int i = 0; i < shareholderTrs.size(); i++) {
						if(i>3){
							Elements shareholderTds = shareholderTrs.get(i).children();
							ReportPrefshareholder reportPrefshareholder = new ReportPrefshareholder();
							reportPrefshareholder.setPrefShareId(shareholderTds.get(0).text());
							reportPrefshareholder.setName(shareholderTds.get(1).text());
							reportPrefshareholder.setBgnShares(shareholderTds.get(2).text());
							reportPrefshareholder.setEndShares(shareholderTds.get(3).text());
							reportPrefshareholder.setEndSharePct(shareholderTds.get(4).text());
							reportPrefshareholder.setProjectScopeId(companyPtrId);
							reportPrefshareholderList.add(reportPrefshareholder);
						}
					}
					reportPrefshareholderRepositoy.save(reportPrefshareholderList);
				}
			}
		}
		//三、债券融资情况
		Element debtfinancing = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.debtfinancing"), reportStockissue, -30);
		if(debtfinancing!=null){
			Element debtfinancingTable = cmpanyPublicInfo.getNextTag(debtfinancing, debtfinancing);
			Elements debtfinancingTrs = debtfinancingTable.select("table tr");
			List<ReportDebtfinancing> reportDebtfinancingList = new ArrayList<ReportDebtfinancing>();
			for (int i = 1; i < debtfinancingTrs.size()-1; i++) {
				Elements debtfinancingTds = debtfinancingTrs.get(i).children();
				ReportDebtfinancing reportDebtfinancing = new ReportDebtfinancing();
				reportDebtfinancing.setSecurityCode(debtfinancingTds.get(0).text());
				reportDebtfinancing.setSecurityAbbr(debtfinancingTds.get(1).text());
				reportDebtfinancing.setType(debtfinancingTds.get(2).text());
				reportDebtfinancing.setProceedsValue(debtfinancingTds.get(3).text());
				reportDebtfinancing.setCouponRate(debtfinancingTds.get(4).text());
				reportDebtfinancing.setDuration(debtfinancingTds.get(5).text());
				reportDebtfinancing.setIsDefaulted(debtfinancingTds.get(6).text());
				reportDebtfinancing.setProjectScopeId(companyPtrId);
				reportDebtfinancingList.add(reportDebtfinancing);
			}
			reportDebtfinancingRepositoy.save(reportDebtfinancingList);
		}
		//债券违约情况
		Element bondDefault = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.bondDefault"), reportStockissue, -35);
		if(bondDefault!=null){
			Element bondDefaultReview = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.seven.bondDefault.end"), bondDefault, "",0);
			reportNeeq.setBondDefaultReview(bondDefaultReview.text());
		}
		//公开发行债券的特殊披露要求
		Element bondPublic = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.bondPublic"), reportStockissue, -45);
		if(bondPublic!=null){
			Element bondPublicReview = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.seven.bondPublic.end"), bondPublic, "",0);
			reportNeeq.setBondPublicDisclosure(bondPublicReview.text());
		}
		//间接融资情况
		Element indirectfinancing = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.indirectfinancing"), reportStockissue, -55);
		if(indirectfinancing!=null){
			Elements indirectfinancingTrs = indirectfinancing.select("table tr");
			List<ReportIndirectfinancing> reportIndirectfinancingList = new ArrayList<ReportIndirectfinancing>();
			for (int i = 1; i < indirectfinancingTrs.size()-1; i++) {
				Elements indirectfinancingTds = indirectfinancingTrs.get(i).children();
				if(indirectfinancingTds.size()==6){
					ReportIndirectfinancing reportIndirectfinancing = new ReportIndirectfinancing();
					reportIndirectfinancing.setMethod(indirectfinancingTds.get(0).text());
					reportIndirectfinancing.setFinancier(indirectfinancingTds.get(1).text());
					reportIndirectfinancing.setProceedsValue(indirectfinancingTds.get(2).text());
					reportIndirectfinancing.setDuration(indirectfinancingTds.get(3).text());
					reportIndirectfinancing.setIsDefaulted(indirectfinancingTds.get(4).text());
					reportIndirectfinancing.setProjectScopeId(companyPtrId);
					reportIndirectfinancingList.add(reportIndirectfinancing);
				}
			}
			reportIndirectfinancingRepositoy.save(reportIndirectfinancingList);
		}
		//违约情况
		Element financing = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.indirect.review"), reportStockissue, -60);
		if(financing!=null){
			Element financingReview = cmpanyPublicInfo.findEndByChar(env.getProperty("pdf.seven.indirect.review.end"), financing, "",0);
			reportNeeq.setIndirectFinancingDefaultReview(financingReview.text());
		}
		//五、分配预案
		Element profitdistribution = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.profitdistribution"), reportStockissue, -65);
		if(profitdistribution!=null){
			Element profitdistributionT = cmpanyPublicInfo.getNextTag(profitdistribution,profitdistribution);
			Elements profitdistributionTrs = profitdistributionT.select("table tr");
			List<ReportProfitdistribution> reportProfitdistributionList = new ArrayList<ReportProfitdistribution>();
			for (int i = 1; i < profitdistributionTrs.size(); i++) {
					Elements profitdistributionTds = profitdistributionTrs.get(i).children();
					if(profitdistributionTds.size()==4){
						ReportProfitdistribution reportProfitdistribution = new ReportProfitdistribution();
						reportProfitdistribution.setDate(profitdistributionTds.get(0).text());
						reportProfitdistribution.setCashDividend10shares(profitdistributionTds.get(1).text());
						reportProfitdistribution.setStockDividend10shares(profitdistributionTds.get(2).text());
						reportProfitdistribution.setTransfer10shares(profitdistributionTds.get(3).text());
						reportProfitdistribution.setProjectScopeId(companyPtrId);
						reportProfitdistributionList.add(reportProfitdistribution);
					}
			}
			reportProfitdistributionRepositoy.save(reportProfitdistributionList);
		}
		//利润分配情况
		Element pdp = cmpanyPublicInfo.findNextByChar(env.getProperty("pdf.seven.bdp"), reportStockissue, -75);
		if(pdp!=null){
			Element pdpT = cmpanyPublicInfo.findNextTag("table",pdp);
			Elements PdpTds = pdpT.select("table tr > *");
			if(PdpTds.size()>6){
			reportNeeq.setPdpCashDividendShares10(PdpTds.get(5).text());
			reportNeeq.setPdpStockDividendShares10(PdpTds.get(6).text());
			reportNeeq.setPdpTransferShares10(PdpTds.get(7).text());
			}
		}
		return reportNeeqRepositoy.save(reportNeeq);
	}
}
